//
//  ListPostViewController.m
//  Doc Bao
//
//  Created by Hai Nguyen on 10/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ListPostViewController.h"
#import "Feed.h"
#import "FeedExtensions.h"
#import "Post.h"
#import "PostExtensions.h"
#import "PostCell.h"

@implementation ListPostViewController
@synthesize postTable;
@synthesize postArray;
@synthesize postCell;

- (void)layoutForTheFeed:(Feed*) theFeed{
	[self.titleLabel setText:theFeed.Name];
	[loadingView setHidden:NO];
	if (postArray == nil) {
		postArray = [[NSMutableArray alloc] init];
	}
	[postArray removeAllObjects];
	[postArray setArray:[Post getPostsWithFeed:[theFeed.Id intValue]]];
	[postTable reloadData];
	[loadingView setHidden: YES];
}

#pragma mark -
#pragma mark UITableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [postArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 77.0f;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *postCellId = @"postCellId";
	postCell = (PostCell*)[postTable dequeueReusableCellWithIdentifier:postCellId];
	if (postCell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"PostCell" owner:self options:nil];
	}
	Post *post = [postArray objectAtIndex:indexPath.row];
	[postCell.thumbnailImage setImage:[UIImage imageWithData:post.Thumbnail]];
	[postCell.titleLabel setText:post.Title];
	[postCell.descriptionTextView setText:post.Description];
	return [postCell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self.view addSubview:loadingView];
	[loadingView setFrame:CGRectMake(0, 44, 320, 367)];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.postTable = nil;
	self.postCell = nil;
}


- (void)dealloc {
	[postTable release];
	[postArray release];
	[postCell release];
    [super dealloc];
}


@end
